package com.houtai.controller.user.info;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.avtivity.works.UserIndexWorksDto;
import com.houtai.bean.dto.user.info.TdUserInfoInsertDto;
import com.houtai.bean.dto.user.info.TdUserInfoUpdateDto;
import com.houtai.bean.dto.user.info.UserIndexDetailDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksListVo;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.user.UserIndexDetailVo;
import com.houtai.bean.vo.user.info.TdUserInfoDetailVo;
import com.houtai.service.dict.DictService;
import com.houtai.service.user.info.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="个人资料接口", tags={"个人资料接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class UserInfoContorller {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private DictService dictService;


    @ApiOperation(value="发起实名认证", notes="发起实名认证")
    @Authority(AuthorityType.User)
    @RequestMapping(value = "/user/info")
    public ResultVo list(@ApiParam(value = "发起实名认证所需参数") @Valid @RequestBody TdUserInfoInsertDto dto, BindingResult result,
                         @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userInfoService.insertTdUserInfo(dto, token);
        }
        return retInfo;
    }

    @ApiOperation(value="用户个人资料", notes="用户个人资料")
    @Authority(AuthorityType.User)
    @RequestMapping(value = "/user/info/detail")
    public TdUserInfoDetailVo list(@RequestHeader String token) {

        return userInfoService.userInfoDetail(token);
    }


    @ApiOperation(value="用户修改资料", notes="用户修改资料")
    @Authority(AuthorityType.UserReal)
    @RequestMapping(value = "/user/info/update")
    public ResultVo update(@ApiParam(value = "用户修改资料所需参数") @Valid @RequestBody TdUserInfoUpdateDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userInfoService.updateTdUserInfo(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="用户主页", notes="用户主页")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/index")
    public UserIndexDetailVo update(@ApiParam(value = "用户主页所需参数") @Valid @RequestBody UserIndexDetailDto dto, BindingResult result,
                                    @RequestHeader String token) {
        UserIndexDetailVo retInfo = new UserIndexDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userInfoService.userIndex(dto.getUser_id());
        }
        return retInfo;
    }
    @ApiOperation(value="用户主页作品列表", notes="用户主页作品列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/works/list")
    public TdActivityWorksListVo works(@ApiParam(value = "用户主页作品列表所需参数") @Valid @RequestBody UserIndexWorksDto dto, BindingResult result,
                                       @RequestHeader String token) {
        TdActivityWorksListVo retInfo = new TdActivityWorksListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userInfoService.userWorksList(dto);
        }
        return retInfo;
    }
    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="职业数据列表", notes="职业数据列表")
    @RequestMapping(value = "/profession/list", method = RequestMethod.POST)
    public DictListResult status() {
        return dictService.findDictByType("profession");
    }
}
